<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }
        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }
        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }
        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }
        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="用户名" v-model="pagination.queryString" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建用户</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="username" label="用户名" align="center"></el-table-column>
                <el-table-column prop="gender" label="性别" align="center"></el-table-column>
                <el-table-column prop="birthday" label="出生日期" align="center"></el-table-column>
                <el-table-column prop="telephone" label="电话号码" align="center"></el-table-column>
                <el-table-column prop="station" label="状态" align="center"></el-table-column>
                <el-table-column prop="remark" label="备注" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="addUser(scope.row)">编辑用户</el-button>
                        <el-button type="primary" size="mini" @click="updateRole(scope.row)">编辑角色</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 添加用户 -->
            <div class="add-form">
                <el-dialog title="新建用户" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="100px"  >
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="用户名" prop="username">
                                    <el-input v-model="formData.username"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="密码" prop="password">
                                    <el-input v-model="formData.password"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="性别">
                                    <el-radio v-model="formData.gender" label="1">男</el-radio>
                                    <el-radio v-model="formData.gender" label="2">女</el-radio>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="出生日期">
                                    <div class="block">
                                        <el-date-picker
                                                v-model="formData.birthday"
                                                type="date"
                                                placeholder="选择日期">
                                        </el-date-picker>
                                    </div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="手机号" prop="telephone">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="状态">
                                    <el-select v-model="formData.station">
                                        <el-option label="0" value="0"></el-option>
                                        <el-option label="1" value="1"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="备注">
                                    <el-input v-model="formData.remark" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>



            <!--编辑用户-->
            <div class="add-form">
                <el-dialog title="编辑用户" :visible.sync="dialogFormUserVisible">
                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right" label-width="70px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="用户名" prop="username">
                                    <el-input v-model="formData.username"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="密码" prop="password">
                                    <el-input v-model="formData.password"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="性别">
                                    <el-radio v-model="formData.gender" label="1">男</el-radio>
                                    <el-radio v-model="formData.gender" label="2">女</el-radio>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="出生日期">
                                    <div class="block">
                                        <el-date-picker
                                                v-model="formData.birthday"
                                                type="date"
                                                placeholder="选择日期">
                                        </el-date-picker>
                                    </div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="手机号" prop="telephone">
                                    <el-input v-model="formData.telephone"/>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="状态">
                                    <el-select v-model="formData.station">
                                        <el-option label="0" value="0"></el-option>
                                        <el-option label="1" value="1"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="备注">
                                    <el-input v-model="formData.remark" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <el-button size="mini"  type="danger" @click="delUser()">删除该用户</el-button>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 编辑角色 -->
            <div class="add-form">
                <el-dialog title="编辑角色" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <div class="checkScrol">
                            <el-button size="mini" style="float:right" type="danger" @click="delRole()">删除用户所有角色</el-button>
                            <table class="datatable">
                                <thead>
                                <tr>
                                    <th>序号</th>
                                    <th>角色名</th>
                                    <th>角色关键字</th>
                                    <th>选择</th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr v-for="(c,i) in tableData">
                                    <td><label :for="c.id">{{i+1}}</label></td>
                                    <td><label :for="c.id">{{c.name}}</label></td>
                                    <td><label :for="c.id">{{c.keyword}}</label></td>
                                    <td>
                                        <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleRole()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data:{
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize:10,
                total:100,
                queryString:null,
            },
            userId:0,   //用户id
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData:[],//添加角色信息窗口中角色信息列表数据
            roleIds:[],//添加表单窗口中检查组复选框对应id
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit:false,//编辑角色
            dialogFormUserVisible:false,    //编辑用户窗口
            //校验规则
            rules: {
                username: [{ required: true, message: '用户名为必填项', trigger: 'blur' }],
                password: [{ required: true, message: '密码为必填项', trigger: 'blur' }],
                telephone: [{ required: true, message: '手机号为必填项', trigger: 'blur' }]
            }
        },
        created() {
            this.findPage();
        },
        methods: {
            //删除用户
            delUser(){
                this.$confirm('你确定要清楚当前用户角色吗？','提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then((response)=>{
                    //发送Ajax
                    axios.post("/user/delUserById.do?id="+this.userId).then((response)=>{
                        if(response.data.flag){
                            this.$message({
                                type:'success',
                                message:response.data.message
                            });
                            this.findPage();
                            this.dialogFormUserVisible=false;
                        }else{
                            //执行失败
                            this.$message.error(response.data.message);
                        }
                    });
                }).catch(() => {
                    this.$message("已取消");
                });
            },
            //保存编辑用户
            handleEdit(){
               //发送ajax请求
               axios.post("/user/edit.do",this.formData).then((response)=>{
                   if(response.data.flag){
                       this.findPage();
                       this.dialogFormUserVisible=false;
                       this.$message.success(response.data.message);
                    }else{
                       this.$message.error(response.data.message);
                    }
               })
            },
            //打开编辑用户窗口
            addUser(row){
                this.dialogFormUserVisible=true;
                //发送ajax请求，回显数据
                axios.post("/user/findById.do?id="+row.id).then((response)=>{
                    if(response.data.flag){
                        this.formData=response.data.data;
                    }else{
                        this.$message.error(response.data.message);
                    }
                });
                this.userId=row.id;
            },
            //编辑角色
            handleRole(row){
               if(this.roleIds.length>0){
                   //发送ajax请求，
                   axios.post("/user/addRole.do?roleIds="+this.roleIds+"&userId="+this.userId).then((response)=>{
                       if(response.data.flag){
                           //添加成功，关闭窗口
                           this.dialogFormVisible4Edit=false;
                           this.$message({
                               type:'success',
                               message:response.data.message
                           });
                           this.findPage()
                        }else{
                           //执行失败，弹出提示信息
                           this.$message.error(response.data.message);
                          }
                   });
               }else{
                   this.$message.error("请选择用户角色！");
               }
            },
            //添加用户
            handleAdd () {
                //进行表单校验
                this.$refs['dataAddForm'].validate((valid) => {
                    if(valid){
                        //如果校验通过，发送ajax请求，将表单数据提交到Controller
                        axios.post("/user/add.do",this.formData).then((response)=>{
                            if(response.data.flag){
                            this.$message({
                                type:'success',
                                message:response.data.message
                            });
                            //关闭新增窗口
                            this.dialogFormVisible = false;
                            //分页查询
                            this.findPage();
                            }else{
                                //处理失败
                                this.$message.error(response.data.message);
                            }
                        })
                    }else{
                        //校验不通过，提示错误信息
                        this.$message.error("表单数据校验失败，请检查输入是否正确！");
                     }
                });
            },
            //分页查询
            findPage() {

                var param = {
                    currentPage:this.pagination.currentPage,
                    pageSize:this.pagination.pageSize,
                    queryString:this.pagination.queryString
                };

                //发送ajax请求，进行分页查询
                axios.post("/user/findPage.do",param).then((res) => {
                    this.pagination.total = res.data.total;
                this.dataList = res.data.rows;
                });


            },
            // 重置表单
            resetForm() {
                this.formData = {};
            },
            // 弹出添加用户窗口
            handleCreate() {
                this.resetForm();
                this.dialogFormVisible=true;
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;//指定最新的页码
                this.findPage();
            },
            //弹出编辑角色窗口
            updateRole(row){
                this.dialogFormVisible4Edit=true;
                this.roleIds=[];
                //发送ajax请求，查询角色信息
                axios.post("/role/findAll.do").then((response)=>{
                    if(response.data.flag){
                        this.tableData=response.data.data;

                        //再次发送ajax请求，查询用户已经拥有的角色
                        axios.post("/role/findRoleIdsByUserId.do?roleIds="+row.id).then((response)=>{
                            if(response.data.flag){
                                this.roleIds = response.data.data;
                            }
                        });
                     }else{
                    this.$message.error(response.data.message);
                    }
                });
                this.userId=row.id;
            },
            //删除角色
            delRole(row){
                //弹出确认框
                this.$confirm('你确定要清楚当前用户角色吗？','提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then((response)=>{
                    //发送ajax
                    axios.post("/user/delRole.do?id="+this.userId).then((response)=>{
                        if(response.data.flag){
                            this.$message({
                                type:'success',
                                message:response.data.message
                            });
                            this.findPage();
                            this.dialogFormVisible4Edit=false;
                        }else{
                            //执行失败
                            this.$message.error(response.data.message);
                        }
                        });
                }).catch(() => {
                        this.$message("已取消");
                });
            }
        }
    })
</script>
</html>